package test;

/**
 * Project		: lwjglTests
 * File 		: camtest.java			Version: 
 * Date 		: 1:49:05 AM
 * Author		: kkopiec
 * Description	:
 *
 */

/**
 * @author kkopiec
 *
 */
import glapp.*;
import glmodel.GLModel;

import org.lwjgl.opengl.*;
import org.lwjgl.input.*;
import org.lwjgl.util.glu.*;
import static org.lwjgl.opengl.GL11.*;

public class camtest extends GLApp {

	float[] lightDir = { -2f, 2f, 2f, 0f };
	static float[] campos = { 0f, 0f, -150f };
	float camRotX = 0f;
	float camRotY = 0f;
	float rotX = 0, rotY = 0, rotZ = 0;
	int pavementDL = 0; // Cube displayList;
	int roadVDL = 0;
	int roadHDL = 0;
	int buildingTexH = 0;
	private GLModel car;
	private GLImage sky;
	private GLModel building;
	// private CameraG3d camera;
	private GLCamera camera;
	boolean mousePressed = false;
	CubeGl road, pavement, grass, build;

	public static void main(String[] args) {
		camtest prog = new camtest();
		prog.window_title = "OGL Test for driving sim by KKopiec";
		prog.displayWidth = 800;
		prog.displayHeight = 600;
		prog.fullScreen = false;
		prog.run();

	}

	public void setup() {
		glEnable(GL_LIGHTING);
		setPerspective();
		setAmbientLight(new float[] { .6f, .6f, .9f, 1f });
		setLight(GL11.GL_LIGHT1, new float[] { 1f, 1f, 1f, 1f }, // diffuse
				// color
				new float[] { .6f, .6f, .9f, 1f }, // ambient
				new float[] { 1f, 1f, 1f, 1f }, // specular
				lightDir);
		car = new GLModel("models/BMW/bmw_full.obj");
		sky = new GLImage("images/sky.jpg");
		car.regenerateNormals();
		building = new GLModel("models/tower1/craptower_10x10x20.obj");
		building.regenerateNormals();
		buildingTexH = makeTexture("images/Glass1.jpg");
		// camera = new CameraG3d(new Vector3f(0f, -5f, 0f));
		camera = new GLCamera();
		camera.setCamera(0.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

		road = new CubeGl(new float[] { 10f, 2.0f, 10f }, new float[] { 0.58f,
				0.58f, 0.58f, 1.0f, 0.1f }, "images/road1.jpg");
		build = new CubeGl(new float[] { 10f, 30.0f, 10f }, new float[] {
				0.58f, 0.58f, 0.58f, 1.0f, 0.8f }, "images/Glass1.jpg");

		roadVDL = beginDisplayList();

		for (int i = 0; i < 20; i++) {
			glTranslatef(0.0f, 0.0f, -10.0f);
			road.render();
		}

		endDisplayList();
		pavement = new CubeGl(new float[] { 15.0f, 2.0f, 50f }, new float[] {
				0.58f, 0.58f, 0.58f, 1.0f, 0.1f }, "images/grass1.jpg");
		pavementDL = beginDisplayList();
		for (int i = 0; i < 20; i++) {
			// glTranslatef(-25.0f, 0.0f, 0.0f);
			glTranslatef(0.0f, 0.0f, -pavement.getLength());
			pavement.render();
		}
		endDisplayList();
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
	}

	public static void setPerspective() {
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		GLU.gluPerspective(60f, aspectRatio, 0.1f, 1000f);
		glMatrixMode(GL_MODELVIEW);

	}

	public void draw() {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		drawImageFullScreen(sky);
		EventsListener();
		camera.Render();
		// Vertical Layout
		glPushMatrix();
		{

			glTranslatef(-100.0f, 0.0f, 0.0f);
			for (int i = 0; i < 10; i++) {
				callDisplayList(roadVDL);
				glTranslatef(20.0f, 0f, 200.0f);
			}
		}

		glPopMatrix();

		// Horizontal Layout
		glPushMatrix();
		{
			glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
			glTranslatef(0.0f, 0.0f, 100.0f);
			for (int i = 0; i < 10; i++) {
				callDisplayList(roadVDL);
				glTranslatef(20.0f, 0f, 200.0f);
			}
		}

		glPopMatrix();

		// Building
		glPushMatrix();
		{
			// glScalef(0.04f,0.04f,0.04f);
			// glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
			// glBindTexture(GL_TEXTURE_2D, buildingTexH);
			// building.render();
			glTranslatef(-110.0f, 0.0f, -10.0f);
			for (int j = 0; j < 10; j++) {
				
				for (int i = 0; i < 10; i++) {
					glTranslatef(20.0f, build.getHeight() / 2, 0.0f);

					build.render();
					glTranslatef(0.0f, -build.getHeight() / 2, 0.0f);
				}
				glTranslatef(-200.0f, 0.0f, -10.0f);
				glTranslatef(0.0f, 0.0f, -10.0f);
			}
		}
		glPopMatrix();

		glFlush();

	}

	public void EventsListener() {
		// Mouse.setCursorPosition(Display.getWidth() / 2, Display.getHeight() /
		// 2); // make
		// Mouse.setGrabbed(true); // mouse
		// pointing
		// in
		// the
		// middle
		// of
		// the
		// screen
		if (Keyboard.getEventKeyState()) {
			if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
				camera.RotateY(1.0f);
				// rotX -= 1.0f;
			}
			if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
				// rotY += 1.0f;
				camera.RotateY(-1.0f);
			}
			if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
				camera.RotateX(1.0f);
				// rotX += 1.0f;
			}
			if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
				camera.RotateX(-1.0f);
				// rotX -=1.0f;
			}
			if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
				camera.MoveForward(-1.0f);
				// rotZ += 1.0f;
			}
			if (Keyboard.isKeyDown(Keyboard.KEY_NUMPAD1)) {
				rotZ -= 1.0f;
			}
			if (rotX > 360.0f)
				rotX -= 360.0f;
			if (rotX < -360.0f)
				rotX += 360.0f;
			if (rotY > 360.0f)
				rotY -= 360.0f;
			if (rotY < -360.0f)
				rotY += 360.0f;
			if (rotZ > 360.0f)
				rotY -= 360.0f;
			if (rotZ < -360.0f)
				rotY += 360.0f;
		}

	}

	public void keyDown(int keycode) {
		if (keycode == (Keyboard.KEY_A)) {
			// camera.left = true;
			System.out.println("Key Down");

		}
		if (keycode == (Keyboard.KEY_D)) {
			// camera.right = true;
			System.out.println("Key Down");
		}
		if (keycode == (Keyboard.KEY_W)) {
			// camera.forward = true;
			System.out.println("Key Down");
		}
		if (keycode == (Keyboard.KEY_S)) {
			// camera.backward = true;
			System.out.println("Key Down");
		}

	}

	public void keyUp(int keycode) {
		if (keycode == (Keyboard.KEY_A)) {
			// camera.left = false;
			System.out.println("Key UP");

		}
		if (keycode == (Keyboard.KEY_D)) {
			// camera.right = false;
			System.out.println("Key UP");
		}
		if (keycode == (Keyboard.KEY_W)) {
			// camera.forward = false;
			System.out.println("Key UP");
		}
		if (keycode == (Keyboard.KEY_S)) {
			// camera.backward = false;
			System.out.println("Key Down");
		}
	}

}
